<script>
import {
  addUploadSettings,
  queryUploadSettings,
  updateUploadSettings
} from "@/api/systemAdministration/uploadTheFile/uploadTheFile";

let logo;//标记是新增数据还是修改数据
export default {
  data(){
    let checkAllowedFileTypes = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('不能为空'));
      }
      setTimeout(() => {
        if (!Number.isInteger(value)) {
          callback(new Error('请输入数字值'));
        } else {
          if (value < 1) {
            callback(new Error('不能小于1'));
          } else if(value > 1024) {
            callback(new Error('不能大于1G'));
          }else{
            callback();
          }
        }
      }, 1000);
    };
    return{
      //修改和新增Form数据
      Form:{
        fileSizeLimit: "",
        allowedFileTypes:[]
      },
      //修改和新增约束
      FormRules: {
        allowedFileTypes:[
          { required: true, message: '必选', trigger: 'blur' },
        ],
        fileSizeLimit:[
          { validator: checkAllowedFileTypes, trigger: 'blur' }
        ],
      },
    }
  },
  mounted(){
    this.getData();
  },
  methods:{
    getData(){
      queryUploadSettings()
        .then((data) => {
          console.log(data);
          if(data.code === 200){
            if(data.data != null){
              this.Form = data.data;
              logo = "update";
            }else{
              logo = "add";
            }
          }
        })
        .catch((error) =>{
          console.log(error);
        })
    },
    onSubmit(){
      this.$refs.FormRef.validate((valid) => {
        if(!valid) {
          this.$message.warning('兄嘚按规矩办事！！')
          return false;
        }else{
          console.log(this.Form);
          if(logo === "update"){
            updateUploadSettings(this.Form)
              .then((data) => {
                if(data.code === 200){
                  this.$message({
                    message: '修改成功！！',
                    type: 'success'
                  });
                  this.getData();
                }else{
                  this.$message.error('修改失败！！');
                }
              })
              .catch((error) => {
                console.log(error);
              })
          }else if(logo === "add"){
            addUploadSettings(this.Form)
              .then((data) => {
                if(data.code === 200){
                  this.$message({
                    message: '新增成功！！',
                    type: 'success'
                  });
                  this.getData();
                }else{
                  this.$message.error('新增失败！！');
                }
              })
              .catch((error) => {
                console.log(error);
              });
          }
        }
      })
    },
  }
}
</script>

<template>
  <div style="margin-top: 20px">
    <el-form
      :model="Form"
      ref="FormRef"
      :rules="FormRules"
      status-icon
      label-position="left"
      style="margin-left: 100px">
      <el-form-item label="文件上传最大值（M）：" label-width="180px" prop="fileSizeLimit">
        <el-input style="width: 180px" v-model.number="Form.fileSizeLimit"/>
      </el-form-item>
      <el-form-item label="文件上传格式：" label-width="180px" prop="allowedFileTypes">
        <el-checkbox-group v-model="Form.allowedFileTypes">
          <el-checkbox label="doc" name="type"></el-checkbox>
          <el-checkbox label="docx" name="type"></el-checkbox>
          <el-checkbox label="xls" name="type"></el-checkbox>
          <el-checkbox label="xlsx" name="type"></el-checkbox>
          <el-checkbox label="ppt" name="type"></el-checkbox>
          <el-checkbox label="rar" name="type"></el-checkbox>
          <el-checkbox label="pdf" name="type"></el-checkbox>
          <el-checkbox label="txt" name="type"></el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">
          保存
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<style scoped lang="scss">

</style>
